using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using WebSMS.Core.ResponseEntity;

namespace WebSMS;

public class PostService : ITPots,ITransient
{
    private readonly IRepository<PostPo> _repo;
    public readonly UserPostLinksService _postPo;
    public IRepository<UserPo> _userPo;
    public IRepository<Department> _departmentPo;
    public IRepository<UserPostLinks> _userPostLinks;
    public PostService(IRepository<PostPo> repository,
        UserPostLinksService userPostLinksService,
        IRepository<UserPo> userPo,
        IRepository<Department> departmentPo,
        IRepository<UserPostLinks> userPostLinks)
    {
        _repo = repository;
        _postPo = userPostLinksService;
        _userPo = userPo;
        _departmentPo = departmentPo;
        _userPostLinks = userPostLinks;
    }
    public async Task<PostPo> GetPostByUserId(Guid userId)
    {
     PostPo post = await _repo.SingleOrDefaultAsync(u => u.Id == userId);
     return post;
    }

    public  PagedList<PostPo> GetPostAll(int page,int size)
    {
        var postList = _repo.AsQueryable().ToPagedList(page, size);
        return postList;
    }
    public PagedList<PostPo> GetAllPostByDepartmentId(Guid departmentId)
    {
        var postList = _repo.Where(p => p.DepartmentId == departmentId).ToPagedList();
        return postList;
    }

    public int GetPostCount()
    {
        var count = _repo.Count();
        return count;
    }

    public async Task<bool> UpdatePost(PostPo post)
    {
       await _repo.UpdateAsync(post);
       return true;
    }

    public async Task<bool> AddPost(PostPo post)
    {
        await _repo.InsertNowAsync(post);
        return true;
    }

    public async Task<bool> DeletePost(Guid postId)
    {
      var status =  await _postPo.RemoveUserForPost(postId);
      if (!status)
      {
         await _repo.DeleteNowAsync(postId);
         return true;
      }
      return status;
    }


    public async Task<object> GetPostDetails(Guid postId)
    {
        // 获取指定岗位的信息
        var post = await _repo.AsQueryable()
            .Where(p => p.Id == postId)
            .Select(p => new PostDto
            {
                PostId = p.Id.Value,
                PostName = p.Name,
                DepartmentId = p.DepartmentId.Value
            })
            .FirstOrDefaultAsync();

        if (post == null)
        {
            return null; // 或者抛出一个异常
        }

        // 获取与该岗位关联的用户链接
        var userPostLinks = await _userPostLinks.AsQueryable()
            .Where(upl => upl.PostId == postId)
            .ToListAsync();

        // 获取所有相关的用户
        var userIds = userPostLinks.Select(upl => upl.UserId).Distinct().ToList();
        var users = await _userPo.AsQueryable()
            .Where(u => userIds.Contains(u.Id))
            .Select(u => new UserDtoP
            {
                UserId = u.Id,
                Username = u.Username,
                Email = u.Email,
                Phone = u.Phone,
              
            })
            .ToListAsync();

        // 获取岗位所属部门的信息
        var department = await _departmentPo.AsQueryable()
            .Where(d => d.Id == post.DepartmentId)
            .FirstOrDefaultAsync();

        if (department == null)
        {
            return null; // 或者抛出一个异常
        }

        // 获取上级部门的信息
        var parentDepartmentName = department.ParentId.HasValue 
            ? await _departmentPo.AsQueryable()
                .Where(pd => pd.Id == department.ParentId)
                .Select(pd => pd.Name)
                .FirstOrDefaultAsync()
            : null;

        // 组装最终结果
        var result = new PostsDetailsResponse
        {
            Departments = new List<DepartmentDto>
            {
                new DepartmentDto
                {
                    DepartmentId = department.Id.Value,
                    DepartmentName = department.Name,
                    ParentDepartmentId = department.ParentId,
                    ParentDepartmentName = parentDepartmentName
                }
            },
            Users = users
        };

        return result;
    }
}